package com.wzcl.app.listen;

import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * @author beingven
 * @Description 用于监听Session的创建移除和销毁
 * @createTime 2020年11月25日 14:35:00
 */
@Log4j2
public class SessionListener implements HttpSessionAttributeListener {
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("-----------------------------session创建----------------------------------");
        log.info("--attributeAdded--");
        log.info("key----:"+httpSessionBindingEvent.getName());
        log.info("value---:"+httpSessionBindingEvent.getValue());
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("-----------------------------session移除----------------------------------");
        log.info("--attributeRemoved--");
        log.info("key----:"+httpSessionBindingEvent.getName());
        log.info("value---:"+httpSessionBindingEvent.getValue());
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("-----------------------------session销毁----------------------------------");
        log.info("--attributeReplaced--");
        String oldName = httpSessionBindingEvent.getName();
        log.info("--old key--"+oldName);
        log.info("--old value--"+httpSessionBindingEvent.getValue());
        HttpSession session = httpSessionBindingEvent.getSession();
        log.info("new value---:"+session.getAttribute(oldName));
    }
}
